using Microsoft.AspNetCore.Mvc;
using QiAdmin.Core;
using QiAdmin.Core.Attributes;
using QiAdmin.Core.Models;
using QiAdmin.Model.Dtos.Input;
using QiAdmin.Service.IService;

namespace QiAdmin.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysFilesController : ControllerBase
    {
        readonly ITSysFilesService _tSysFilesService;
        public SysFilesController(ITSysFilesService tSysFilesService)
        {
            _tSysFilesService = tSysFilesService;
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetPageList(TSysFiles_IN input)
        {
            var res = _tSysFilesService.GetPageList(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Add)]
        [HttpPost]
        public ApiJsonResult Add([FromForm] TSysFiles_IN input)
        {
            var res = _tSysFilesService.Add(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Edit)]
        [HttpPost]
        public ApiJsonResult Edit(TSysFiles_IN input)
        {
            var res = _tSysFilesService.Edit(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult Get(TSysFiles_IN input)
        {
            var res = _tSysFilesService.Get(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Delete)]
        [HttpPost]
        public ApiJsonResult RemoveRange(List<TSysFiles_IN> input)
        {
            var res = _tSysFilesService.RemoveRange(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public IActionResult DownLoad(string fileName)
        {
            var path = _tSysFilesService.AdminDownLoad(fileName);
            if (path.IsSuccess)
            {
                FileStream fileStream = new FileStream(path.Result, FileMode.Open);
                FileStreamResult streamResult = new FileStreamResult(fileStream, "application/octet-stream")
                {
                    FileDownloadName = fileName
                };
                return streamResult;
            }
            else
            {
                return Content(path.Msg);
            }
        }
    }
}
